package com.example.util;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

@Component
public class SensitiveWordFilter {
    private static final String REPLACE_WORD = "*";

    private Map<String, Object> sensitiveWordMap = null;

    @PostConstruct
    private void init() {
        Set<String> sensitiveWordSet = new HashSet<>();
        try {
            Resource resource = new ClassPathResource("sensitive_words_lines.txt");
            BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream(),"UTF-8"));
            String line;
            while ((line = br.readLine()) != null) {
                sensitiveWordSet.add(line.trim());
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 将敏感词库加入到HashMap中
        addSensitiveWordToHashMap(sensitiveWordSet);
    }

    private void addSensitiveWordToHashMap(Set<String> sensitiveWordSet) {
        sensitiveWordMap = new HashMap<>(sensitiveWordSet.size());
        String key;
        Map<String, Object> nowMap;
        Map<String, Object> newWordMap;
        Iterator<String> iterator = sensitiveWordSet.iterator();
        while (iterator.hasNext()) {
            key = iterator.next();
            nowMap = sensitiveWordMap;
            for (int i = 0; i < key.length(); i++) {
                char keyChar = key.charAt(i);
                Object wordMap = nowMap.get(String.valueOf(keyChar));
                if (wordMap != null) {
                    nowMap = (Map<String, Object>) wordMap;
                } else {
                    newWordMap = new HashMap<>();
                    newWordMap.put("isEnd", "0");
                    nowMap.put(String.valueOf(keyChar), newWordMap);
                    nowMap = newWordMap;
                }
                if (i == key.length() - 1) {
                    nowMap.put("isEnd", "1");
                }
            }
        }
        System.out.println(sensitiveWordMap);
    }

    public String filter(String txt) {
        if (StringUtils.isEmpty(txt)) {
            return txt;
        }
        StringBuilder resultTxt = new StringBuilder();
        Map<String, Object> nowMap = sensitiveWordMap;
        int begin = 0;
        int position = 0;
        StringBuilder wordBuffer = new StringBuilder();
        while (position < txt.length()) {
            char word = txt.charAt(position);
            System.out.println(word);
            nowMap = (Map<String, Object>) nowMap.get(String.valueOf(word));
            System.out.println(nowMap);
            if (nowMap != null) {
                wordBuffer.append(word);
                position++;
                if ("1".equals(nowMap.get("isEnd"))) {
                    resultTxt.append(repeat(REPLACE_WORD,wordBuffer.length()));
                    begin = position;
                    nowMap = sensitiveWordMap;
                    wordBuffer.setLength(0);
                }
            } else {
                resultTxt.append(txt.charAt(begin));
                position = begin + 1;
                begin = position;
                nowMap = sensitiveWordMap;
                wordBuffer.setLength(0);
            }
        }
        resultTxt.append(txt.substring(begin));
        return resultTxt.toString();
    }

    public String repeat(String str, int times) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < times; i++) {
            sb.append(str);
        }
        return sb.toString();
    }
}
